package br.ufrj.ppgi.greco.controller;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.ufrj.ppgi.greco.model.dao.DAODynamicDataProperty;
import br.ufrj.ppgi.greco.model.dao.DAOProperty;
import br.ufrj.ppgi.greco.model.dao.DAOResource;
import br.ufrj.ppgi.greco.model.dao.DAOService;
import br.ufrj.ppgi.greco.model.dao.DAOWebHook;
import br.ufrj.ppgi.greco.model.entity.EntityDynamicDataProperty;
import br.ufrj.ppgi.greco.model.entity.EntityResource;
import br.ufrj.ppgi.greco.model.entity.EntityService;
import br.ufrj.ppgi.greco.model.entity.EntityWebHook;

@Resource
public class DynamicDataPropertyController
{
	private DAOResource dr;
	private DAOWebHook dw;
	private DAODynamicDataProperty dd;
	private DAOService ds;
	private Result result;
	private DAOProperty dp;
	
	
	public DynamicDataPropertyController(DAOProperty dp,DAOResource dr, DAODynamicDataProperty dd, DAOService ds,DAOWebHook dw , Result result)
	{
		this.dr = dr;
		this.dp = dp;
		this.dd = dd;
		this.ds = ds;
		this.dw = dw;
		this.result = result;
	}
	
	
	@Get
	@Path("/config/resources/{id}/dynamicDataProperties/new")
	public void newDynamicDataProperty(int id)
	{
		this.result.include("id",id);
		
		this.result.include("properties",this.dp.getAllPropertiesJson());
		this.result.include("services",this.ds.getAllServicesJson());
		this.result.include("hooks", this.dw.getAllHooksJson());
	}
	
	@Post
	@Path("/config/resources/{id}/dynamicDataProperties/create")
	public void createDynamicDataProperty(EntityDynamicDataProperty ddp, String url, int id)
	{
		EntityResource er = this.dr.getByID(id);
		ddp.setResource(er);
		

		if(ddp.getSourceType().equals("service"))
		{
			EntityService es = this.ds.getServiceByURL(url);
			ddp.setService(es);
		}
		else 	
		{
			EntityWebHook ew = this.dw.getHookByURL(url);
			ddp.setWebHook(ew);
		}
		
		
		this.dd.insert(ddp);
		
	
		er.getDynamicDataProperties().add(ddp);
		this.dr.update(er);
		
			
		this.result.redirectTo(ResourceController.class).listResource(id);
	}
	
	@Get
	@Path("/config/resources/{id}/dynamicDataProperties/{idDynamicDP}")
	public void listDynamicDataProperty(int id, int idDynamicDP)
	{
		this.result.include("dynamic",this.dd.getByID(idDynamicDP));
		this.result.include("id",id);
	
		this.result.include("properties",this.dp.getAllPropertiesJson());
		this.result.include("services",this.ds.getAllServicesJson());
		this.result.include("hooks", this.dw.getAllHooksJson());
	}
	
	@Post
	@Path("/config/resources/{id}/dynamicDataProperties/update")
	public void updateDynamicDataProperty(EntityDynamicDataProperty ddp,int id, String url)
	{
		ddp.setResource(this.dr.getByID(id));
		
		if(ddp.getSourceType().equals("service"))
		{
			EntityService es = this.ds.getServiceByURL(url);
			ddp.setService(es);
		}
		else 	
		{
			EntityWebHook ew = this.dw.getHookByURL(url);
			ddp.setWebHook(ew);
		}
		
		this.dd.update(ddp);
		this.result.redirectTo(ResourceController.class).listResource(id);
	}
	
	@Get
	@Path("/config/resources/{resourceId}/dynamicDataProperties/{idDynamic}/delete")
	public void deleteDynamicDataProperty(int resourceId, int idDynamic)
	{
		this.dd.deleteByID(idDynamic);
		this.result.redirectTo(ResourceController.class).listResource(resourceId);
	}

}
